package zabbix

import (
	"encoding/json"
	"errors"
)

func UserCheckAuthenticationData() (string,map[string]interface{}) {
	method := "user.checkAuthentication"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "sessionid": "8C8447FF6F61D134CEAC740CCA1BC90D"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserCreateData() (string,map[string]interface{}) {
	method := "user.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "alias": "John",
        "passwd": "Doe123",
        "usrgrps": [
            {
                "usrgrpid": "7"
            }
        ],
        "user_medias": [
            {
                "mediatypeid": "1",
                "sendto": [
                    "support@company.com"
                ],
                "active": 0,
                "severity": 63,
                "period": "1-7,00:00-24:00"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserDeleteData() (string,map[string]interface{}) {
	method := "user.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "1",
        "5"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserGetData() (string,map[string]interface{}) {
	method := "user.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserLoginData() (string,map[string]interface{}) {
	method := "user.login"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "user": "Admin",
        "password": "zabbix",
        "userData": true
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserLogoutData() (string,map[string]interface{}) {
	method := "user.logout"
	var data map[string]interface{}
	jsonstr := []byte(`[

	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func UserUpdateData() (string,map[string]interface{}) {
	method := "user.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "userid": "1",
        "name": "John",
        "surname": "Doe"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func UserCheckAuthentication(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserCheckAuthenticationData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserLogin(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserLoginData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserLogout(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserLogoutData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func UserUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := UserUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



